﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace _0812045
{
    public class MousePointer : DrawableGameComponent
    {
        private SpriteBatch spriteBatch;
        public Vector2 MousePosition;

        private MouseState CurrentMouseState;
        private MouseState PreviousMouseState;

        private Color pointerColor = Color.White;

        private Texture2D[] pointerTexture;

        public Texture2D[] PointerTexture
        {
            get { return pointerTexture; }
            set { pointerTexture = value; }
        }

        private int index;

        public int Index
        {
            get { return index; }
            set { index = value; }
        }

        private Rectangle restrictZone;

        public Rectangle RestrictZone
        {
            get { return restrictZone; }
            set { restrictZone = value; }
        }

        public MousePointer(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }


        ///

        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        ///
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                // TODO: Load any ResourceManagementMode.Automatic content
                this.spriteBatch = new SpriteBatch(this.GraphicsDevice);
            }
            
            // TODO: Load any ResourceManagementMode.Manual content
            base.LoadGraphicsContent(loadAllContent);
        }

        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            base.UnloadGraphicsContent(unloadAllContent);
        }

        ///

        /// Allows the game component to update itself.
        ///
        /// Provides a snapshot of timing values.
        int delay = 0;
        public override void Update(GameTime gameTime)
        {
            if(delay == 0)
                index = (index + 1) % 8;
            delay = (delay + 1) % 3; 
            // TODO: Add your update code here
            this.CurrentMouseState = Mouse.GetState();
            this.MousePosition.X = this.CurrentMouseState.X - pointerTexture[0].Width / 2;
            this.MousePosition.Y = this.CurrentMouseState.Y - pointerTexture[0].Height / 2;

            // Restrict the Mouse so that it stays inside the current display
            if (this.MousePosition.X < 0)
                this.MousePosition.X = 0;
            if (this.MousePosition.X > this.restrictZone.Width - pointerTexture[0].Width)
                this.MousePosition.X = this.restrictZone.Width - pointerTexture[0].Width;
            if (this.MousePosition.Y < 0)
                this.MousePosition.Y = 0;
            if (this.MousePosition.Y > this.restrictZone.Height - pointerTexture[0].Width)
                this.MousePosition.Y = this.restrictZone.Height - pointerTexture[0].Width;

            this.PreviousMouseState = this.CurrentMouseState;

            base.Update(gameTime);
        }

        ///

        /// Allows the game commponent to Draw itself
        ///
        /// Provides a snapshot of timing values
        public override void Draw(GameTime gameTime)
        {
            this.spriteBatch.Begin();
            this.spriteBatch.Draw(this.pointerTexture[index], this.MousePosition, this.pointerColor);
            this.spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
